<?php
/* 报名任务 */
namespace Api\Controller;
use Think\Controller;
use Think\Log;
class PEnterTaskController extends Controller {
    private $user = array();

    public function __construct()
	{
		//解决跨域
		echoHeader();
		$phone = I('post.phone', '', 'trim');
		// $phone = '13660392546';
        $this->user = session('p_user');
		if(!$this->user or $this->user['phone'] != $phone){
			$this->ajaxReturn(array('err_code'=>10000, 'err_msg'=>'请先登录'));
		}
        $this->user = M("PUser")->where(array("phone"=>$phone, "is_deleted"=>0))->find();
		if (!$this->user) {
			$this->ajaxReturn(array('err_code'=>10000, 'err_msg'=>'请先登录'));
		}
        $this->user['score']=user_score_sum($this->user['id']);
	}

	public function index()
	{
		$map['is_deleted']=0;
		$pageSize = 100;
		$pageIndex = I('post.page', 1, 'intval');
		$total = M("PTask")->where($map)->count();

		$page = $this->pagination($total, $pageSize ,$pageIndex);

		if($total){
			$list = M("PTask")->field('id,title,description,start_time,end_time,enter_count,finish_count,url,encourage')->where($map)
					->limit($page['offset'], $page['limit'])->order('id desc')->select();

            unset($map);
            $map['user_id'] = $this->user['id'];

	    	$now = time();

		    foreach ($list as $k => $v) {
                $map['task_id'] = $v['id'];
				$list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);
				$list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
				$list[$k]['end_time'] = date('Y-m-d H:i', $v['end_time']);

                $list[$k]['enter_time']='';
                $list[$k]['finish_time']='';
                $list[$k]['scope']=0;
                $list[$k]['remaining_time']=($v['end_time']-$now);
                $list[$k]['status']=0;     //代表任务未开始
                if($list[$k]['remaining_time']<0)
                    $list[$k]['remaining_time']=0;
                $act = M("PTaskPerson")->where($map)->find();

                if ($act) {
                    $list[$k]['enter_time']=date('Y-m-d H:i', $act['add_time']);
                    if($act['finish_time']>1)
                        $list[$k]['finish_time']=date('Y-m-d H:i', $act['finish_time']);
                    $list[$k]['scope']=$act['scope'];
                    $list[$k]['status']=2;

                    if($act['finish_time']>1){    //已完成任务
                        if($act['scope']>0)
                            $list[$k]['status']=3;
                        else
                            $list[$k]['status']=4;
                    }
                    else if($now>$v['end_time']){  //任务未完成
//                        $list[$k]['status']=4;
                        $list[$k]['status']=5;
                    }
                }
                else{
                    if($now>$v['end_time']){  //任务过期了
                        $list[$k]['status']=5;
                    }
                    else if($v['start_time']<$now){     //已经开始了
                        $list[$k]['status']=1;
                    }

                }

                //活动 0为未到任务时间 ，1为已经开始了 并且未领任务 ，2为领取了任务 ，  3，为完成了任务
                //4 为任务失败 5为任务过期


            }

		}else{
			$list = array();
		}
		$this->ajaxReturn(array('err_code'=>200, 'err_msg'=>array('list'=>$list,'page'=>$page)));

	}

	private function pagination($total, $pageSize, $pageIndex)
	{
	    $page = array();
	    $totalPages = ceil($total / $pageSize);
	    $pageIndex <= 0 and $pageIndex = 1;
	    $offset = ($pageIndex - 1) * $pageSize;

	    $page['limit'] = $pageSize;
	    $page['offset'] = $offset;
	    $page['total'] = (int)$total;//总条目数
	    $page['total_pages'] = (int)$totalPages;//总页数
	    $page['current_page'] = $pageIndex;//当前页数
	    $page['has_nextpage'] = $totalPages > $pageIndex;//是否还有下一页
	    return $page;
	}

	//报名
	public function enter(){

        $id = I('post.id', 0, 'intval');
        $map['id'] = $id;
        $map['is_deleted'] = 0;

        $act = M("PTask")->where($map)->find();
        if (!$act) {
            $this->ajaxReturn(array('err_code'=>10001, 'err_msg'=>'活动不存在'));
        }

		$now = time();
		if($now < $act['start_time']){
			$this->ajaxReturn(array('err_code'=>20001, 'err_msg'=>'报名活动尚未开放'));
		}

		if($now > $act['end_time']){
			$this->ajaxReturn(array('err_code'=>20002, 'err_msg'=>'活动已结束'));
		}


        $enter_count=$act['enter_count'];
        unset($map);

        $recMap['task_id'] = $id;
        $recMap['user_id'] = $this->user['id'];
        $count = M("PTaskPerson")->where($recMap)->count();
        if ($count) {
        	$this->ajaxReturn(array('err_code'=>10002, 'err_msg'=>'您已报名该活动'));
        }
        M()->startTrans();

        $recMap['add_time'] = time();
        $res = M("PTaskPerson")->add($recMap);
        if (!$res) {
            M()->rollback();
            return $this->ajaxReturn(array('err_code'=>50000, 'err_msg'=>'活动报名失败，请稍后再试'));
        }
        unset($recMap);

        $recMap['id'] = $id;
        $recMap['is_deleted'] = 0;
        $recMap['enter_count'] = $enter_count+1;


        $res = M("PTask")->save($recMap);
        if ($res !== false) {
        	M()->commit();
            return $this->ajaxReturn(array('err_code'=>0, 'err_msg'=>'报名成功'));
        }else{
             M()->rollback();
            return $this->ajaxReturn(array('err_code'=>50000, 'err_msg'=>'报名活动失败'));
        }

    }

	
}